﻿using Coldairarrow.Entity.DTO;
using Coldairarrow.Entity.PB;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace Coldairarrow.Business.PB
{
    public partial class PB_ReturnDetailBusiness : BaseBusiness<PB_ReturnDetail>, IPB_ReturnDetailBusiness, ITransientDependency
    {
        public PB_ReturnDetailBusiness(IDbAccessor db)
            : base(db)
        {
        }

        #region 外部接口

        public async Task<PageResult<PB_ReturnDetail>> GetDataListAsync(PageInput<ConditionDTO> input)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<PB_ReturnDetail>();
            var search = input.Search;

            //筛选
            if (!search.Condition.IsNullOrEmpty() && !search.Keyword.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<PB_ReturnDetail, bool>(
                    ParsingConfig.Default, false, $@"{search.Condition}.Contains(@0)", search.Keyword);
                where = where.And(newWhere);
            }

            return await q.Where(where).GetPageResultAsync(input);
        }
        /// <summary>
        /// 资产回货损失统计
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PageResult<AssetLossStatisticsDTO>> GetDataTotalDTOListAsync(PageInput<AssetLossStatisticsConditionDTO> input)
        {
            string str = @"SELECT projectName,Buildingnumber,MaterialCode,MaterialName,MaterialSpec,SUM(DeliverNums)DeliverNums,SUM(ReturnNums)ReturnNums,SUM(LossQuantity)LossQuantity,Weight,SUM(LossWeight)LossWeight from (
            SELECT d.Name projectName,c.Buildingnumber,a.MaterialCode,a.MaterialName,a.MaterialSpec,a.DeliverNums,a.ReturnNums,a.DeliverNums-a.ReturnNums LossQuantity,ifnull(e.Weight,0) Weight,(a.DeliverNums-a.ReturnNums)*ifnull(e.Weight,0) LossWeight  from PB_ReturnDetailLog a 
            left join PB_Return b on a.ParentId=b.id
            left join PB_Buildingnumber c on a.BuildingId=c.id
            left join PB_Project d on a.ProjectId=d.id
            left join PB_Material e on a.MaterialId=e.id
            where b.State=1 and a.Deleted=0 and b.Deleted=0) m where 1=1 ";
            if (!input.Search.projectName.IsNullOrEmpty())
            {
                str += " and projectName like '%" + input.Search.projectName + "%' ";
            }
            if (!input.Search.Buildingnumber.IsNullOrEmpty())
            {
                str += " and Buildingnumber like '%" + input.Search.Buildingnumber + "%' ";
            }
            if (!input.Search.MaterialCode.IsNullOrEmpty())
            {
                str += " and MaterialCode like '%" + input.Search.MaterialCode + "%' ";
            }
            if (!input.Search.MaterialName.IsNullOrEmpty())
            {
                str += " and MaterialName like '%" + input.Search.MaterialName + "%' ";
            }
            if (!input.Search.MaterialSpec.IsNullOrEmpty())
            {
                str += " and MaterialSpec like '%" + input.Search.MaterialSpec + "%' ";
            }
            str += " GROUP BY projectName,Buildingnumber,MaterialCode,MaterialName,MaterialSpec,Weight ";
            str += @" union all
            SELECT '合  计'projectName,''Buildingnumber,''MaterialCode,''MaterialName,''MaterialSpec,''DeliverNums,''ReturnNums,''LossQuantity,''Weight,SUM(LossWeight)LossWeight from (
            SELECT d.Name projectName,c.Buildingnumber,a.MaterialCode,a.MaterialName,a.MaterialSpec,a.DeliverNums,a.ReturnNums,a.DeliverNums-a.ReturnNums LossQuantity,ifnull(e.Weight,0) Weight,(a.DeliverNums-a.ReturnNums)*ifnull(e.Weight,0) LossWeight  from PB_ReturnDetailLog a 
            left join PB_Return b on a.ParentId=b.id
            left join PB_Buildingnumber c on a.BuildingId=c.id
            left join PB_Project d on a.ProjectId=d.id
            left join PB_Material e on a.MaterialId=e.id
            where b.State=1 and a.Deleted=0 and b.Deleted=0) m where 1=1 ";
            if (!input.Search.projectName.IsNullOrEmpty())
            {
                str += " and projectName like '%" + input.Search.projectName + "%' ";
            }
            if (!input.Search.Buildingnumber.IsNullOrEmpty())
            {
                str += " and Buildingnumber like '%" + input.Search.Buildingnumber + "%' ";
            }
            if (!input.Search.MaterialCode.IsNullOrEmpty())
            {
                str += " and MaterialCode like '%" + input.Search.MaterialCode + "%' ";
            }
            if (!input.Search.MaterialName.IsNullOrEmpty())
            {
                str += " and MaterialName like '%" + input.Search.MaterialName + "%' ";
            }
            if (!input.Search.MaterialSpec.IsNullOrEmpty())
            {
                str += " and MaterialSpec like '%" + input.Search.MaterialSpec + "%' ";
            }
            var lidata = await Db.GetListBySqlAsync<AssetLossStatisticsDTO>(str);
            int num = lidata.Count;
            var list = lidata
               .Skip((input.PageIndex - 1) * input.PageRows)
               .Take(input.PageRows).ToList();
            return new PageResult<AssetLossStatisticsDTO> { Data = list, Total = num };
        }
        public async Task<PB_ReturnDetail> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }

        public async Task AddDataAsync(PB_ReturnDetail data)
        {
            await InsertAsync(data);
        }

        public async Task UpdateDataAsync(PB_ReturnDetail data)
        {
            await UpdateAsync(data);
        }

        public async Task DeleteDataAsync(List<string> ids)
        {
            await DeleteAsync(ids);
        }

        #endregion

        #region 私有成员

        #endregion
    }
}